package com.acme.superchat.service.client;

import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author Alexander Kotov (kotov.alex.22@gmail.com)
 */
public class ClientMessageReceiver implements Runnable {
    private BufferedReader bufferedReader;
    private Thread motherThread;

    public ClientMessageReceiver(BufferedReader bufferedReader, Thread motherThread) {
        this.bufferedReader = bufferedReader;
        this.motherThread = motherThread;
    }

    @Override
    public void run() {
        try {
            while (true) {
                String message = bufferedReader.readLine();
                System.out.println(message);
            }
        } catch (IOException e) {
            try {
                bufferedReader.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            System.out.println("Connection was lost!");
            motherThread.stop();
        }
    }
}
